const express = require('express')
const app = express()
const bodyParser = require('body-parser')

app.all('*', function (req, res, next) {
  console.log("äpi tests Access-Control cros")
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Credentials', true);
  res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
  next();
});


app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

// Require API routes
const users = require('./routes/users')
const test = require('./routes/test')
const api = require('./api')

// Import API Routes
app.use(users)
app.use(test)
app.use(api)
// Export express app
module.exports = app

console.log("äpi tests", require.main === module)
// Start standalone server if directly running
if (require.main === module) {
  const port = process.env.PORT || 3001
  app.listen(port, () => {
    // eslint-disable-next-line no-console
    console.log(`API server listening on port ${port}`)
  })
}
